/*
 * @Description:
 * @Author: 郑灶欣
 * @Date: 2021-11-23 08:45:14
 * @LastEditors: Seven
 * @LastEditTime: 2021-12-11 13:02:43
 * @FilePath: \koga-manager\src\api\request\index.js
 */
import axios from "axios";
import { getToken, setToken, removeToken } from "@/utils/auth";

// create an axios instance
const service = axios.create({
    timeout: 6000 * 10, // request timeout
});

// request interceptor
service.interceptors.request.use(
    (config) => {
        if (getToken()) {
            config.headers["token"] = getToken();
        }
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);

// response interceptor
service.interceptors.response.use(
    (res) => {
        if (res.headers.token) {
            setToken(res.headers.token);
        }
        // 未设置状态码则默认成功状态
        const code = res.data.resultCode;
        if (code === -9999 || code === -99999) {
            //提示 todo
            removeToken();
            window.location.href = window.location.origin;
        } else {
            if (code !== 10000) {
                //提示 todo
            }
            return res.data;
        }
    },
    (error) => {
        //提示 todo
        return Promise.reject(error);
    }
);

export default service;